package com.molichuxing.gateway.api.services.impl;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.gateway.api.services.LogApiService;
import com.molichuxing.gateway.bff.log.entity.response.DrivingLicenseNotifyLogVo;
import com.molichuxing.gateway.bff.log.entity.response.IdverifyNotifyLogVo;
import com.molichuxing.gateway.bff.log.entity.response.ThreeverifyNotifyLogVo;
import com.molichuxing.gateway.bff.log.services.LogBffService;
import com.molichuxing.gateway.bff.stock.entity.response.StockCarVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = "第三方日志相关接口")
@RestController
@RequestMapping(value = "/log")
public class LogApiServiceImpl extends RootApiService implements LogApiService{

	private static final Logger logger = LoggerFactory.getLogger(LogApiServiceImpl.class);
	
	@Resource
	private LogBffService logBffService;
	
	/**
	 * 获取身份验证日志列表
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "身份证日志列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "tabType", value = "tab类型", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "idNumber", value = "身份证号", required = false, dataType = "String"),
			@ApiImplicitParam(name = "realName", value = "用户姓名", required = false, dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String"),
			@ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")
	})
	@GetMapping(value = "/idverify/list")
	public ResponseResult<Paged<IdverifyNotifyLogVo>> getIdverifyNotifyLogList(@RequestParam Map<String, Object> map) {
		
		ResponseResult<Paged<IdverifyNotifyLogVo>> result = new ResponseResult<>();
		try {
			Integer pageNum = ObjectCastUtil.castInteger(map.get("pageNum"));
			if(null==pageNum || pageNum < 1 ) {
				map.put("pageNum",1);
			}
			Integer pageSize = ObjectCastUtil.castInteger(map.get("pageSize"));
			if(null==pageSize || pageSize <= 0) {
				map.put("pageSize",10);
			}
			String startTime = ObjectCastUtil.castString(map.get("startTime"));
			if(StringUtils.isNoneBlank(startTime)) {
				map.put("startTime",DateUtil.parseDate(startTime).atStartOfDay());
			}
			String endTime = ObjectCastUtil.castString(map.get("endTime"));
			if(StringUtils.isNoneBlank(endTime)) {
				map.put("endTime", DateUtil.getDayEnd(DateUtil.parseDate(endTime).atStartOfDay()));
			}
			Paged<IdverifyNotifyLogVo> idverifyNotifyLogList = logBffService.getIdverifyNotifyLogList(map);
			return result.success(idverifyNotifyLogList);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getIdverifyNotifyLogList]获取身份验证日志列表异常", e);
			result.error("获取身份验证日志列表信息异常");
		}
		return result;
	}
	
	/**
	 * 获取三要素日志列表
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "三要素日志列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "tabType", value = "tab类型", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "idNumber", value = "身份证号", required = false, dataType = "String"),
			@ApiImplicitParam(name = "realName", value = "用户姓名", required = false, dataType = "String"),
			@ApiImplicitParam(name = "userPhone", value = "用户手机号", required = false, dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String"),
			@ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")
	})
	@GetMapping(value = "/threeverify/list")
	public ResponseResult<Paged<ThreeverifyNotifyLogVo>> getThreeverifyNotifyLogList(@RequestParam Map<String, Object> map) {
		
		ResponseResult<Paged<ThreeverifyNotifyLogVo>> result = new ResponseResult<>();
		try {
			Integer pageNum = ObjectCastUtil.castInteger(map.get("pageNum"));
			if(null==pageNum || pageNum < 1 ) {
				map.put("pageNum",1);
			}
			Integer pageSize = ObjectCastUtil.castInteger(map.get("pageSize"));
			if(null==pageSize || pageSize <= 0) {
				map.put("pageSize",10);
			}
			String startTime = ObjectCastUtil.castString(map.get("startTime"));
			if(StringUtils.isNoneBlank(startTime)) {
				map.put("startTime",DateUtil.parseDate(startTime).atStartOfDay());
			}
			String endTime = ObjectCastUtil.castString(map.get("endTime"));
			if(StringUtils.isNoneBlank(endTime)) {
				map.put("endTime", DateUtil.getDayEnd(DateUtil.parseDate(endTime).atStartOfDay()));
			}
			Paged<ThreeverifyNotifyLogVo> idverifyNotifyLogList = logBffService.getThreeverifyNotifyLogList(map);
			return result.success(idverifyNotifyLogList);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getThreeverifyNotifyLogList]获取三要素日志列表异常", e);
			result.error("获取三要素日志列表信息异常");
		}
		return result;
	}
	
	/**
	 * 获取驾驶证日志列表
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "驾驶证日志列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "tabType", value = "tab类型", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "idNumber", value = "身份证号", required = false, dataType = "String"),
			@ApiImplicitParam(name = "realName", value = "用户姓名", required = false, dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String"),
			@ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")
	})
	@GetMapping(value = "/drivingLicense/list")
	public ResponseResult<Paged<DrivingLicenseNotifyLogVo>> getDrivingLicenseNotifyLogList(@RequestParam Map<String, Object> map) {
		
		ResponseResult<Paged<DrivingLicenseNotifyLogVo>> result = new ResponseResult<>();
		try {
			Integer pageNum = ObjectCastUtil.castInteger(map.get("pageNum"));
			if(null==pageNum || pageNum < 1 ) {
				map.put("pageNum",1);
			}
			Integer pageSize = ObjectCastUtil.castInteger(map.get("pageSize"));
			if(null==pageSize || pageSize <= 0) {
				map.put("pageSize",10);
			}
			String startTime = ObjectCastUtil.castString(map.get("startTime"));
			if(StringUtils.isNoneBlank(startTime)) {
				map.put("startTime",DateUtil.parseDate(startTime).atStartOfDay());
			}
			String endTime = ObjectCastUtil.castString(map.get("endTime"));
			if(StringUtils.isNoneBlank(endTime)) {
				map.put("endTime", DateUtil.getDayEnd(DateUtil.parseDate(endTime).atStartOfDay()));
			}
			Paged<DrivingLicenseNotifyLogVo> idverifyNotifyLogList = logBffService.getDrivingLicenseNotifyLogList(map);
			return result.success(idverifyNotifyLogList);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getDrivingLicenseNotifyLogList]获取驾驶证日志列表异常", e);
			result.error("获取驾驶证日志列表信息异常");
		}
		return result;
	}
	
}
